package miage.ecom.commandes;


import java.io.PrintStream;
import java.util.Iterator;
import java.util.Set;
import miage.ecom.Main.Shell;
import miage.ecom.Shell.ShellContext;

    


public class VarCommandImpl implements ShellCommand
    {
    private ShellContext _context = null;
    public VarCommandImpl(ShellContext context)
    {
    _context = context;
    }
    /**
    * renvoie le nom de la commande
    * @return
    */
    @Override
    public String getName()
    {
    String langue = (String) _context.getVar(Shell.LANGUAGE);
     if(langue.equals("fr")) {
    
    return "variable";}
    if(langue.equals("es")) {
    return "variable";}
    if(langue.equals("it")) {
    return "variabile";}
    else{
    //default anglais
    return "variable";
    }
    }

    /**
    * renvoie le nom de la commande
    * @return
    */
    @Override
    public String getUsage()
    {
    String langue = (String) _context.getVar(Shell.LANGUAGE);
     if(langue.equals("fr")) {
    
    return "variable";}
    if(langue.equals("es")) {
    return "variable";}
    if(langue.equals("it")) {
    return "variabile";}
    else{
    //default anglais
    return "variable";
    }
    }
    /**
    * renvoie la description de la commande
    * @return
    */
    @Override
    public String getShortDescription()
    {
    String langue = (String) _context.getVar(Shell.LANGUAGE);
    if(langue.equals("fr")) {
    
    return "liste les variables";}
    if(langue.equals("es")) {
    return "las variables de la lista";}
    if(langue.equals("it")) {
    return "lista delle variabili";}
    else{
    //default anglais
    return "list the variables.";}
    }
    

    /**
    * execute la commande
    * @param cmdline
    * @param out
    * @param err
    */
    @Override
    public void execute(String cmdline, PrintStream out, PrintStream err) throws Exception
    {
    Set varNames = _context.getVarNames();
    Iterator iterator=varNames.iterator();
    if(iterator==null) return;
    StringBuffer sb = new StringBuffer();
    while(iterator.hasNext()){
    String key=(String)iterator.next();	
    Object value=_context.getVar(key);
    sb.append(key);
    sb.append('=');
    if(value!=null)	sb.append(value.toString());
    sb.append('\n');
    }
    out.println(sb);
    }
    }
